package com.itheima.web.servlet;
import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class brandServlet extends BaseServlet {
    private BrandService brandService = new BrandServiceImpl();
    public  void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.调用service查询
        List<Brand> brands = brandService.SelectAll();
        //2.转为Json
        String jsonString = JSON.toJSONString(brands);
        //3.写数据
        response.setContentType("text/json;charset=utf-8");//因为存在中文情况，故此需设置请求头
        response.getWriter().write(jsonString);
    }
    public  void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);
        //调用service添加
        brandService.add(brand);
        //响应成功标识
        response.getWriter().write("success");
    }
    public  void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为Brand对象
        int[] ids = JSON.parseObject(params, int[].class);
        //调用service添加
        brandService.deleteByIds(ids);
        //响应成功标识
        response.getWriter().write("success");
    }
    public  void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收参数 调用service查询
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        PageBean<Brand> pageBean = brandService.seletByPage(currentPage,pageSize);
        //2.转为Json
        String jsonString = JSON.toJSONString(pageBean);
        //3.写数据
        response.setContentType("text/json;charset=utf-8");//因为存在中文情况，故此需设置请求头
        response.getWriter().write(jsonString);
    }
    public  void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收参数 调用service查询
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        //获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);

        PageBean<Brand> pageBean = brandService.seletByPageAndCondition(currentPage,pageSize,brand);
        //2.转为Json
        String jsonString = JSON.toJSONString(pageBean);
        //3.写数据
        response.setContentType("text/json;charset=utf-8");//因为存在中文情况，故此需设置请求头
        response.getWriter().write(jsonString);
    }
    public  void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收品牌数据
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);
        System.out.println(id);
        //转为Brand对象
        //int[] ids = JSON.parseObject(params, int[].class);
        //调用service添加
        brandService.deleteById(id);
        //响应成功标识
        response.getWriter().write("success");
    }
    public  void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //System.out.println(params);
        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);
        //调用service添加
        brandService.update(brand);
        //响应成功标识
        response.getWriter().write("success");
    }
    public  void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收参数 调用service查询
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);
        Brand brand = brandService.selectById(id);
        //2.转为Json
        String jsonString = JSON.toJSONString(brand);
        //3.写数据
        response.setContentType("text/json;charset=utf-8");//因为存在中文情况，故此需设置请求头
        response.getWriter().write(jsonString);
    }
}
